/**
 * 
 */
package com.example.sjt.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.sjt.dao.UserRepository;
import com.example.sjt.model.User;

/**
 * @author yuqy
 *
 */
@RestController
@RequestMapping("/auth")
public class AuthController {

	@Autowired
	private UserRepository userRepository;

	@Autowired
	private BCryptPasswordEncoder bCryptPasswordEncoder;

	@PostMapping("/register")
	public String registerUser(@RequestBody Map<String, String> registerUser) {
		User user = userRepository.findByUsername(registerUser.get("username"));
		if (user == null) {
			user = new User();
			user.setUsername(registerUser.get("username"));
			user.setPassword(bCryptPasswordEncoder.encode(registerUser.get("password")));
			user.setRole("ROLE_USER");
			User save = userRepository.save(user);
			return save.toString();
		} else {
			return user.toString();
		}
	}

}
